﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {

	public float m_LinerSpeed;  
	public GameObject m_StarObj;  

    private enum MoveState
    {
        Circle = 1,
        Line = 2,
    }
    private MoveState m_moveState = MoveState.Circle;

	private float m_radius;  
	private Rigidbody2D m_body;   
	private float m_omga;
    private Vector3 m_circleDot;

	// Use this for initialization  
	void Start () {  
		m_body=gameObject.GetComponent<Rigidbody2D> ();
		m_body.velocity = new Vector3(-m_LinerSpeed, 0, 0);
	}  

	// Update is called once per frame  
	void Update () {  
		if (Input.GetMouseButtonUp(0))
        {
            m_moveState = MoveState.Line;
        }
	}  

	void FixedUpdate () {  
        if (m_moveState == MoveState.Circle)
        {
            m_circleDot = m_StarObj.transform.position;
            m_radius = (m_circleDot - gameObject.transform.position).magnitude;
            m_omga = m_LinerSpeed * m_LinerSpeed / m_radius;

            Vector3 m_force = m_circleDot - gameObject.transform.position;//向心力矢量，但此时向量模不正确  
            m_force = m_force.normalized * m_body.mass * m_omga;//纠正向量的模  
            m_body.AddForce(m_force, ForceMode2D.Force);

            transform.Rotate(new Vector3(0, 0, -180 * m_LinerSpeed / (m_radius * Mathf.PI)) * Time.deltaTime);
            //transform.LookAt(m_StarObj.transform, Vector3.back);
        }
    }

    public void SetLineMode()
    {
        m_moveState = MoveState.Line;
    }

    public void SetCircleMode()
    {
        m_moveState = MoveState.Circle;
    }

}
